﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using UserDefineForm.control;


namespace UserDefineForm.formcomponent
{
    public partial class ToolBox : UserControl
    {
        private DesignPanel designPnl;
        private PropertiesPanelv2 propPnl;
        private bool isCreatingControl;

        public ToolBox(DesignPanel designPnl, PropertiesPanelv2 propPnl)
        {
            this.designPnl = designPnl;
            this.propPnl= propPnl;
            InitializeComponent();

            addMouseDownHandler();
            addMouseHoverHandler();
        }

        #region drag and drop

        private void addMouseDownHandler()
        {
            textBoxImg.MouseDown += new MouseEventHandler(img_MouseDown);
            numberImg.MouseDown += new MouseEventHandler(img_MouseDown);
            dateTimeImg.MouseDown += new MouseEventHandler(img_MouseDown);
            checkBoxImg.MouseDown += new MouseEventHandler(img_MouseDown);
            radioImg.MouseDown += new MouseEventHandler(img_MouseDown);
            comboBoxImg.MouseDown += new MouseEventHandler(img_MouseDown);

            textBoxImg.MouseUp += new MouseEventHandler(img_MouseUp);
            numberImg.MouseUp += new MouseEventHandler(img_MouseUp);
            dateTimeImg.MouseUp += new MouseEventHandler(img_MouseUp);
            checkBoxImg.MouseUp += new MouseEventHandler(img_MouseUp);
            radioImg.MouseUp += new MouseEventHandler(img_MouseUp);
            comboBoxImg.MouseUp += new MouseEventHandler(img_MouseUp);
        }

        private void img_MouseDown(object sender, MouseEventArgs e)
        {
            
            isCreatingControl = true;
        }

        private void img_MouseUp(object sender, MouseEventArgs e)
        {
            if (isCreatingControl == false) return;
            isCreatingControl = false;
            PictureBox currentPic = (PictureBox)sender;
        //    int newX = e.X + currentPic.Location.X - designPnl.Location.X;
        //    int newY = e.Y + currentPic.Location.Y - designPnl.Location.Y;

            Point picScreenPoint = currentPic.PointToScreen(Point.Empty);
            Point designPnlScreenPoint = designPnl.PointToScreen(Point.Empty);

            int newX = e.X + picScreenPoint.X - designPnlScreenPoint.X;
            int newY = e.Y + picScreenPoint.Y - designPnlScreenPoint.Y;

            if (mouseUpAtCorrectLocation(newX, newY))
            {

                DesignableControlBase creatingControl = getDragControl(currentPic);
                if (creatingControl != null)
                {

                    addControlToDesignPanel(creatingControl, newX, newY);
                    propPnl.setControl(creatingControl);
                }
            }
        }

        private void addControlToDesignPanel(DesignableControlBase creatingControl, int newX, int newY)
        {
            designPnl.addDesignableControl(creatingControl, newX, newY);

        }


        private bool mouseUpAtCorrectLocation(int X, int Y)
        {
            if (X >= 0 && X < designPnl.Size.Width && Y > 0 && Y < designPnl.Size.Height)
            {
                return true;
            }
            return false;
        }

        private DesignableControlBase getDragControl(PictureBox pic)
        {
            if (pic == textBoxImg)
            {
                return new DesignableTextBox(designPnl,propPnl);
            }
            else if (pic == numberImg)
            {
                return new DesignableNumbericTextBox(designPnl, propPnl);
            }
            else if (pic == checkBoxImg)
            {
                return new DesignableCheckBox(designPnl, propPnl);
            }
            else if (pic == radioImg)
            {
                return new DesignableRadioButton(designPnl, propPnl);
            }
            else if (pic == dateTimeImg)
            {
                return new DesignableDatePicker(designPnl, propPnl);
            }
            else if (pic == comboBoxImg)
            {
                return new DesignableComboBox(designPnl, propPnl);
            }
            else return null;
        }

        #endregion

        #region mouse hover effect
        private void addMouseHoverHandler()
        {
            textBoxImg.MouseHover += new EventHandler(img_MouseHover);
            numberImg.MouseHover += new EventHandler(img_MouseHover);
            dateTimeImg.MouseHover += new EventHandler(img_MouseHover);
            checkBoxImg.MouseHover += new EventHandler(img_MouseHover);
            radioImg.MouseHover += new EventHandler(img_MouseHover);
            comboBoxImg.MouseHover += new EventHandler(img_MouseHover);

            textBoxImg.MouseLeave += new EventHandler(img_MouseLeave);
            numberImg.MouseLeave += new EventHandler(img_MouseLeave);
            dateTimeImg.MouseLeave += new EventHandler(img_MouseLeave);
            checkBoxImg.MouseLeave += new EventHandler(img_MouseLeave);
            radioImg.MouseLeave += new EventHandler(img_MouseLeave);
            comboBoxImg.MouseLeave += new EventHandler(img_MouseLeave);
        }

        void img_MouseLeave(object sender, EventArgs e)
        {
            PictureBox pic = (PictureBox)sender;
            pic.BorderStyle = BorderStyle.None;
        }

        void img_MouseHover(object sender, EventArgs e)
        {
            PictureBox pic = (PictureBox)sender;
            pic.BorderStyle = BorderStyle.Fixed3D;
        }
        #endregion
    }
}
